﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MvcApplication4.Models;

namespace MvcApplication4.Controllers
{
    public class AccountController : Controller
    {

        private ForumsEntities2 db = new ForumsEntities2();
        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View();
        }


        /********** REGISTER *************/
        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();// ContextDependentView();
        }

        [HttpPost]
        public ActionResult Register(MvcApplication4.Models.AccountModel.RegisterModel user)
        {
            User newUser = new User();
            newUser.UserName = user.UserName;
            newUser.Password = user.Password;
            newUser.Email = user.Email;
            newUser.Rating = 0;
            newUser.Avatar = 1;
            newUser.JoinDate = DateTime.Now.ToUniversalTime();
            newUser.NumberOfPosts = 0;
            
            User tempUser = new User();
            try
            {
                tempUser = db.Users.Single(u => u.UserName == user.UserName);
            }
            catch (InvalidOperationException e)
            {
                if (ModelState.IsValid)
                {
                    db.Users.AddObject(newUser);
                    db.SaveChanges();
                    FormsAuthentication.SetAuthCookie(newUser.UserName, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Register Failed");
                }


            }
            ModelState.AddModelError("", "Username is taken.");
            return View(user);
        }

        // Login
        /***************************************************************/
        [AllowAnonymous]
        public ActionResult Login()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Login(MvcApplication4.Models.AccountModel.LoginModel user)
        {

            if (ModelState.IsValid)
            {
                try
                {

                    User tempUser = db.Users.Single(u => u.UserName == user.UserName);
                    if ((tempUser != null) && (tempUser.Password == user.Password))
                    { // logged in
                        FormsAuthentication.SetAuthCookie(tempUser.UserName, false);
                        string oldPage = (string)Session[MvcApplication4.Controllers.SaveVarsController.Session_LastKnownPage];
                        if (oldPage != null && oldPage != "")
                            return Redirect(oldPage);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    { // login failed
                        ModelState.AddModelError("", "Username and password do not match.");
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Username does not exist.");
                }
            }
            return View(user);
        }

        // Logout
        /**************************************************************/
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            string oldPage = (string)Session[MvcApplication4.Controllers.SaveVarsController.Session_LastKnownPage];
            if (oldPage != null && oldPage != "")
                return Redirect(oldPage);
            return RedirectToAction("Index", "Home");
        }
    }
}
